package com.itheima.service.db.impl;

import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.itheima.domain.db.Admin;
import com.itheima.domain.db.UserInfo;
import com.itheima.mapper.AdminMapper;
import com.itheima.mapper.UserInfoMapper;
import com.itheima.service.db.AdminService;
import com.itheima.vo.PageBeanVo;
import org.apache.dubbo.config.annotation.DubboService;
import org.springframework.beans.factory.annotation.Autowired;

@DubboService
public class AdminServiceImpl implements AdminService {

    @Autowired
    private AdminMapper adminMapper;

    @Autowired
    private UserInfoMapper userInfoMapper;
    //根据用户名密码查找对象
    @Override
    public Admin findByUsernameAndPassword(String username, String password) {
        LambdaQueryWrapper<Admin> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(Admin::getUsername, username);
        wrapper.eq(Admin::getPassword, password);
        Admin admin = adminMapper.selectOne(wrapper);
        return admin;
    }

    //用户登陆
    @Override
    public Admin userLogin(String username, String password) {
        //1.创建条件
        LambdaQueryWrapper<Admin> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(Admin::getUsername,username );
        wrapper.eq(Admin::getPassword,password );
        //2.查询
        Admin admin = adminMapper.selectOne(wrapper);
        //3.返回
        return admin;
    }

    //分页查询用户列表
    @Override
    public PageBeanVo findUserListByPage(Integer pageNum, Integer pageSize, String name) {
        //1.创建条件
        LambdaQueryWrapper<UserInfo> wrapper = new LambdaQueryWrapper<>();
        wrapper.like(StrUtil.isNotEmpty(name),UserInfo::getNickname,name);
        //构建分页对象
        Page<UserInfo> page = new Page<>(pageNum, pageSize);
        //2.查询
        page = userInfoMapper.selectPage(page, wrapper);
        //3.赋值pagebeanvo 返回
        return new PageBeanVo(pageNum,pageSize,page.getTotal(),page.getRecords());
    }
}
